package com.zl.roomdesign.main.mime

import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.zl.core.MainApp
import com.zl.roomdesign.main.ImageEntity

/**
 *
 *<p></p>
 *
 * Created by zhangli on 2018/5/7 14:23.<br/>
 */
class MyCompositionAdapter(private var list: MutableList<ImageEntity>, private var onItemClick: ((ViewHolder) -> Unit)? = null) : RecyclerView.Adapter<MyCompositionAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val imageView = ImageView(parent.context)
        return ViewHolder(imageView)
    }

    override fun getItemCount() = list.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val url = MainApp.instance.getServerHost() + list[position].fileName

        Glide.with(holder.itemView)
                .load(url)
                .into(holder.itemView as ImageView)

        holder.itemView.setOnClickListener {
            onItemClick?.invoke(holder)
        }

        holder.itemView.setOnLongClickListener {
            onItemLongClick?.invoke(holder)
            true
        }
    }

    var onItemLongClick: ((ViewHolder) -> Unit)? = null

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view)
}